package com.anche.cywebnew.Utils.query;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpUtil
{
	public static String post(String urls, String token, String data)
	{
		OutputStream output = null;
		OutputStreamWriter writer = null;
		BufferedReader reader = null;
		StringBuffer buffer = new StringBuffer();
        
		try
		{
			URL url = new URL( urls );
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setRequestMethod( "POST" );
			conn.setConnectTimeout( 0 );
			conn.setInstanceFollowRedirects( true );
			conn.setRequestProperty("Accept", "*/*");
			conn.setRequestProperty("Connection", "Keep-Alive");
			conn.setRequestProperty("X-Access-Token", token.trim());
			conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
			conn.setRequestProperty("Content-Type", "application/json;charset=utf-8");
			conn.setDefaultUseCaches( false );
			conn.setDoOutput( true );
			conn.setDoInput( true );
			conn.connect();
			
			output = conn.getOutputStream();
			writer = new OutputStreamWriter(output, "UTF-8");
			writer.write(data);
			writer.flush();
			
			if (conn.getResponseCode() == 200)
			{
				reader = new BufferedReader( new InputStreamReader( conn.getInputStream(), "UTF-8" ) );
				String line = null;
				while ((line = reader.readLine()) != null)
				{
					buffer.append(line);
				}
			}
			conn.disconnect();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				if (null != reader)
				{
					reader.close();
				}
				
				if (null != writer)
				{
					writer.close();
				}
				
				if (null != output)
				{
					output.close();
				}
			}
			catch (Exception e)
			{
				e.printStackTrace();
			}
		}
		
		return buffer.toString();
	}
	
	public static String post(String urls, String data)
	{
		OutputStream output = null;
		OutputStreamWriter writer = null;
		BufferedReader reader = null;
		StringBuffer buffer = new StringBuffer();
        
		try
		{
			URL url = new URL( urls );
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setRequestMethod( "POST" );
			conn.setConnectTimeout( 0 );
			conn.setInstanceFollowRedirects( true );
			conn.setRequestProperty("Accept", "*/*");
			conn.setRequestProperty("Connection", "Keep-Alive");
			conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
			conn.setRequestProperty("Content-Type", "application/json;charset=utf-8");
			conn.setDefaultUseCaches( false );
			conn.setDoOutput( true );
			conn.setDoInput( true );
			conn.connect();
			
			output = conn.getOutputStream();
			writer = new OutputStreamWriter(output, "UTF-8");
			writer.write(data);
			writer.flush();
			
			if (conn.getResponseCode() == 200)
			{
				reader = new BufferedReader( new InputStreamReader( conn.getInputStream(), "UTF-8" ) );
				String line = null;
				while ((line = reader.readLine()) != null)
				{
					buffer.append(line);
				}
			}
			conn.disconnect();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				if (null != reader)
				{
					reader.close();
				}
				
				if (null != writer)
				{
					writer.close();
				}
				
				if (null != output)
				{
					output.close();
				}
			}
			catch (Exception e)
			{
				e.printStackTrace();
			}
		}
		
		return buffer.toString();
	}
}